/**
 * 自定义Hooks【获取】我的问卷 - 问卷列表
 * @author 滕轶男
 */
import { useRequest } from 'ahooks'
import { useSearchParams } from 'react-router-dom'
import { getQuestionListServices } from '../services/question'

import {
    LIST_SEARCH_PARAM_KEY,
    LIST_PAGE_PARAM_KEY,
    LIST_PAGE_SIZE_PARAM_KEY,
    LIST_PAGE_SIZE,
} from '../constant/index'
import { Random } from 'mockjs'

type OptionType = {
    isStar: boolean
    isDeleted: boolean
}
function useLoadQuestionListData(opt: Partial<OptionType> = {}) {
    const { isStar, isDeleted } = opt
    const [searchParams] = useSearchParams()
    const { loading, data, error, refresh } = useRequest(
        async () => {
            const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || ''
            const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY) || '') || 1
            const pageSize =
                parseInt(searchParams.get(LIST_PAGE_SIZE_PARAM_KEY) || '') || LIST_PAGE_SIZE
            const data = await getQuestionListServices({
                keyword,
                isStar,
                isDeleted,
                page,
                pageSize,
            })
            return data
        },
        {
            // 刷新依赖
            refreshDeps: [searchParams],
        }
    )
    return { loading, data, error, refresh }
}

export default useLoadQuestionListData
